// //手动引入封装的axios  network
import network from '../network/index'

//相当于index.js里面的一个小组件（模块）
const module = {
    state: {
        navs: [],
        goods: [],
        allprice:0,
        cart: JSON.parse(localStorage.getItem('carts')) || [],//保存到本地carts数组中
        allSelected: JSON.parse(localStorage.getItem('allSelected')) || false,
    },
    mutations: {
        getCategory(state, items) {
            state.navs = items;
        },
        getGoods(state, items) {
            state.goods = items;
        },
        addcarts(state, items) {
            var flag = false;//判断保存到本地商品中是否已有商品
            state.cart.map((item) => {
                if (item.carts.id == items.id) {
                    flag = true;
                    item.num++;
                }
            })

            if (!flag) {
                state.cart.push({
                    carts: items,
                    num: 1,
                    checked: false,
                })
            }

            localStorage.setItem('carts', JSON.stringify(state.cart));

        },
        // 点击上面的复选框
        changeItemStatus(state, items) {
            state.allprice = 0
            state.cart.map(res=>{
                if(res.checked == true){
                    state.allprice += res.carts.originalPrice * res.num
                }
            })
            
            //逐个点击单选后实现与全选同步
            if(state.cart.every(ele => {
                return ele.checked
            })){
                state.allSelected = true
            }else{
                state.allSelected = false
            }
            localStorage.setItem('carts', JSON.stringify(state.cart));
            localStorage.setItem("allSelected", JSON.stringify(state.allSelected))
        },
        // 点击下面的复选框
        changeAllselect(state, boolean) {
            state.allSelected = boolean;
            state.cart.map(ele => {
                ele.checked = boolean;
            })
            state.allprice = 0
            state.cart.map(res=>{
                if(res.checked == true){
                    state.allprice += res.carts.originalPrice * res.num
                }
            })
            localStorage.setItem('carts', JSON.stringify(state.cart));
            localStorage.setItem("allSelected", JSON.stringify(state.allSelected))
        },
        add(state, item) {
 
            item.num++;
            localStorage.setItem('carts', JSON.stringify(state.cart));
            state.allprice = 0
            state.cart.map(res=>{
                if(res.checked == true){
                    state.allprice += res.carts.originalPrice * res.num
                }
            })
        },
        sub(state, item) {

            if (item.num > 1) {
                item.num--;
            }
            localStorage.setItem('carts', JSON.stringify(state.cart));
            state.allprice = 0
            state.cart.map(res=>{
                if(res.checked == true){
                    state.allprice += res.carts.originalPrice * res.num
                }
            })
        }
    },
    actions: {
        getCategory(context) {
            //调用getlist
            network.getcategory({

            })
                .then(res => {
                    // this.barginlist = res.data.data.goodsMap;
                    // commit('需要触发的Mutation函数')
                    context.commit('getCategory', res.data.data.data)
                })
                .catch(error => {
                    window.console.log(error);
                });
        },
        getGoods(context) {
            //调用getlist
            network.getgoods({
                //    pageId:1,
                //    pageSize:10
            })
                .then(res => {
                    // this.barginlist = res.data.data.goodsMap;
                    // commit('需要触发的Mutation函数')
                    context.commit('getGoods', res.data.data.data.list)
                })
                .catch(error => {
                    window.console.log(error);
                });
        },
        addcarts(context, items) {
            context.commit('addcarts', items)
        },
        // 上面的复选框
        changeItemStatus(context, items) {
            context.commit('changeItemStatus', items)
        },
        changeAllselect(context, boolean) {
            context.commit('changeAllselect', boolean);
        },
        add(context, item) {
            context.commit('add', item)
        },
        sub(context, item) {
            context.commit('sub', item)
        }
    },
    modules: {

    }
}

//抛出module
export default module;
